# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from absl import app, flags

import pandas
import matplotlib
matplotlib.use('Agg')

FLAGS = flags.FLAGS


def cleanup_spread_and_gen_normalized(df, csv_path):
  df.dropna(subset=['avg_bid_ask_spread', 'total_volume', 'vwap'], how='any', inplace=True)
  # df['avg_bid_ask_spread/vwap'] = df['avg_bid_ask_spread/vwap'] * 10000
  df['avg_bid_ask_spread/vwap'] = pandas.Series(
      ["{0:4.3f}".format(val * 10000) for val in df['avg_bid_ask_spread/vwap']], index=df.index)

  df.rename(columns={'avg_bid_ask_spread/vwap': 'avg_bid_ask_spread/vwap_bp'}, inplace=True)
  output_csv_path = '%s.normalized' % csv_path
  df.to_csv(output_csv_path, float_format='%12.6f', index=False)


def read_csv_into_df(csv_path):
  df = pandas.read_csv(csv_path, sep=',', header=0)
  return df


def main(argv):
  csv_path = FLAGS.csv_path
  assert csv_path, '--csv_path must be specified.'

  spread_df = read_csv_into_df(csv_path)
  cleanup_spread_and_gen_normalized(spread_df, csv_path)

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_path', None, 'Input csv file path.')

  app.run(main)
